package com.gdpu.project.action.achievementManager;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.util.DateUtil;
import com.gdpu.project.vo.CgTreatise;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class WritingsAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpSession session;
	private int id;
	private String treatiseId;
	private String publishDate;
	private String teacherId;
	private String teacherName;
	private String treatiseTitle;
	private String publishAdress;
	private String publishDw;
	private String treatiseType;
	private String ziShu;
	private String language;
	private String yiWen;
	private String xueKe;
	private String schoolSign;
	private String isbn;
	private float keYanFen;
	private String comment;
	private String pressLevel;
	private String editorRole;
	private TeacherService teacherService;

	private ChengGuoService chengguoService;
	private PageView<CgTreatise> pageView;
	private int currentpage=1;

	public PageView<CgTreatise> getPageView() {
		return pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getTreatiseId() {
		return treatiseId;
	}

	public void setTreatiseId(String treatiseId) {
		this.treatiseId = treatiseId;
	}

	public String getPublishDate() {
		return publishDate;
	}

	public void setPublishDate(String publishDate) {
		this.publishDate = publishDate;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getTreatiseTitle() {
		return treatiseTitle;
	}

	public void setTreatiseTitle(String treatiseTitle) {
		this.treatiseTitle = treatiseTitle;
	}

	public String getPublishAdress() {
		return publishAdress;
	}

	public void setPublishAdress(String publishAdress) {
		this.publishAdress = publishAdress;
	}

	public String getPublishDw() {
		return publishDw;
	}

	public void setPublishDw(String publishDw) {
		this.publishDw = publishDw;
	}
	public String getPressLevel() {
		return pressLevel;
	}
	
	public void setPressLevel(String pressLevel) {
		this.pressLevel = pressLevel;
	}

	public String getTreatiseType() {
		return treatiseType;
	}

	public void setTreatiseType(String treatiseType) {
		this.treatiseType = treatiseType;
	}

	public String getZiShu() {
		return ziShu;
	}

	public void setZiShu(String ziShu) {
		this.ziShu = ziShu;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getYiWen() {
		return yiWen;
	}

	public void setYiWen(String yiWen) {
		this.yiWen = yiWen;
	}

	public String getXueKe() {
		return xueKe;
	}

	public void setXueKe(String xueKe) {
		this.xueKe = xueKe;
	}

	public String getSchoolSign() {
		return schoolSign;
	}

	public void setSchoolSign(String schoolSign) {
		this.schoolSign = schoolSign;
	}

	public String getIsbn() {
		return isbn;
	}

	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}

	public float getKeYanFen() {
		return keYanFen;
	}

	public void setKeYanFen(float keYanFen) {
		this.keYanFen = keYanFen;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getEditorRole() {
		return editorRole;
	}

	public void setEditorRole(String editorRole) {
		this.editorRole = editorRole;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	// 增加专著
	public String addWritings() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		//Teacher t=(Teacher)session.getAttribute("teacher");
		CgTreatise cgTreatise = new CgTreatise();
		cgTreatise.setComment(getComment());
		cgTreatise.setEditorRole(editorRole);
		cgTreatise.setPressLevel(pressLevel);
		cgTreatise.setTeacherName(teacherName);
		cgTreatise.setIsbn(getIsbn());
		cgTreatise.setKeYanFen(getKeYanFen());
		cgTreatise.setLanguage(getLanguage());
		cgTreatise.setPublishAdress(getPublishAdress());
		cgTreatise.setPublishDate(DateUtil.stringToDate(getPublishDate()));
		cgTreatise.setPublishDw(getPublishDw());
		cgTreatise.setSchoolSign(getSchoolSign());
		cgTreatise.setTeacherId(getTeacherId());
		cgTreatise.setTreatiseId(getTreatiseId());
		cgTreatise.setTreatiseTitle(getTreatiseTitle());
		cgTreatise.setTreatiseType(getTreatiseType());
		cgTreatise.setXueKe(getXueKe());
		cgTreatise.setYiWen(getYiWen());
		cgTreatise.setZiShu(getZiShu());
		//找到对应的教师编号设进去（高级搜索根据teacherId匹配）
		List<Teacher> teacherList=this.teacherService.findByName(teacherName);
		if(teacherList.size()>0){
			Teacher teacher=teacherList.get(0);
			cgTreatise.setTeacherId(teacher.getTeacherId());
		}
		chengguoService.addCgTreatise(cgTreatise);
		List<CgTreatise> cgTreatiselist=this.findAllWritingsDepRole();
		request.setAttribute("cgTreatiselist", cgTreatiselist);
		return SUCCESS;
	}

	

	// 根据Id查找专著
	public String findWritingsById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgTreatise cgTreatise = chengguoService.findCgTreatise(getId());
		request.setAttribute("cgTreatise", cgTreatise);
		return SUCCESS;
	}

	// 根据Id删除专著
	public String delWritingsById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		chengguoService.deleteCgTreatiseById(getId());
		pageView=new PageView<CgTreatise>(10,currentpage);
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgTreatise> cgTreatiseslist=this.findAllWritingsDepRole();
		request.setAttribute("cgTreatiselist", cgTreatiseslist);
		return SUCCESS;
	}

	// 查找所有专著
	public String findAllWritings() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();

		List<CgTreatise> cgTreatiselist = findAllWritingsDepRole();
		request.setAttribute("cgTreatiselist", cgTreatiselist);
		return SUCCESS;
	}

	// 更新专著
	public String updateWritings() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgTreatise cgTreatise = chengguoService.findCgTreatise(getId());
		if (cgTreatise != null) {
			cgTreatise.setPressLevel(getPressLevel());
			cgTreatise.setEditorRole(getEditorRole());
			cgTreatise.setTeacherName(getTeacherName());
			cgTreatise.setComment(getComment());
			cgTreatise.setIsbn(getIsbn());
			cgTreatise.setKeYanFen(getKeYanFen());
			cgTreatise.setLanguage(getLanguage());
			cgTreatise.setPublishAdress(getPublishAdress());
			cgTreatise.setPublishDate(DateUtil.stringToDate(getPublishDate()));
			cgTreatise.setPublishDw(getPublishDw());
			cgTreatise.setSchoolSign(getSchoolSign());
			cgTreatise.setTeacherId(getTeacherId());
			cgTreatise.setTreatiseId(getTreatiseId());
			cgTreatise.setTreatiseTitle(getTreatiseTitle());
			cgTreatise.setTreatiseType(getTreatiseType());
			cgTreatise.setXueKe(getXueKe());
			cgTreatise.setYiWen(getYiWen());
			cgTreatise.setZiShu(getZiShu());
			//找到对应的教师编号设进去（高级搜索根据teacherId匹配）
			List<Teacher> teacherList=this.teacherService.findByName(teacherName);
			if(teacherList.size()>0){
				Teacher teacher=teacherList.get(0);
				cgTreatise.setTeacherId(teacher.getTeacherId());
			}
			chengguoService.updateCgTreatise(cgTreatise);
			//request.setAttribute("cgTreatise", cgTreatise);
		}
		List<CgTreatise> cgTreatiseslist=this.findAllWritingsDepRole();
		request.setAttribute("cgTreatiselist", cgTreatiseslist);
		return SUCCESS;
	}
		
	/**
	 * 角色判断，管理员返回所有，教职工返回相应的
	 * @return
	 */
		private List<CgTreatise> findAllWritingsDepRole() {
			Teacher teacher=(Teacher)session.getAttribute("teacher");
			pageView=new PageView<CgTreatise>(10,currentpage);
			
			QueryResult<CgTreatise> queryResult =null;
			if(teacher.getRole()==1){
			queryResult = chengguoService.findAllCgTreatise(pageView.getFirstResult(),pageView.getMaxresult());
			}else{
				queryResult = chengguoService.findAllCgTreatiseByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
			}
			pageView.setQueryResult(queryResult);
			request.setAttribute("pageView", pageView);
			return queryResult.getResultlist();
		}

}
